package com.xbdun.service.bmp.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xbdun.common.api.Client;
import com.xbdun.common.page.Pager;
import com.xbdun.dao.bmp.dto.SysUserDto;
import com.xbdun.dao.bmp.dto.open.SysOrgFiledsDto;
import com.xbdun.dao.bmp.dto.open.UserNameAndImgUrlDto;
import com.xbdun.dao.bmp.entity.XbdunSysUserDo;
import com.xbdun.dao.bmp.mapper.OrgUserMapper;
import com.xbdun.dao.bmp.mapper.SysOrgMapper;
import com.xbdun.dao.bmp.mapper.SysUserMapper;
import com.xbdun.facade.bmp.request.system.SysUserPagerRequest;
import com.xbdun.facade.bmp.request.system.SysUserRequest;
import com.xbdun.facade.bmp.response.open.OrgUserSaoInfoWayResponse;
import com.xbdun.facade.bmp.response.system.QuerySysUsersResponse;
import com.xbdun.facade.bmp.response.system.SysUserResponse;
import com.xbdun.service.bmp.biz.SysUserBiz;
import com.xbdun.service.bmp.service.cache.OrgInfoCacheService;
import com.xbdun.service.bmp.service.system.SysUserService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	SysUserBiz sysUserBiz;
	@Autowired
	SysUserMapper sysUserMapper;
	@Autowired
	SysOrgMapper sysOrgMapper;
	@Autowired
	OrgUserMapper orgUserMapper;
	@Autowired
	OrgInfoCacheService orgInfoCacheService;
	public SysUserResponse getSysUserLogin(SysUserRequest request)throws Exception {
		
		XbdunSysUserDo xbdunSysUserDo = new XbdunSysUserDo();
		
		xbdunSysUserDo.setUserCode((NumberUtils.isDigits(request.getUserCode())?Long.valueOf(request.getUserCode()):0));
		xbdunSysUserDo.setUnionid(request.getUnionid());//用户微信小程序UnionId
		return sysUserBiz.getSysUserLogin(xbdunSysUserDo);
	}
	
	public Pager<QuerySysUsersResponse> querySysUserPager(SysUserPagerRequest request, Client client) {
		Pager<QuerySysUsersResponse> resultPager = request.newPager(QuerySysUsersResponse.class);
		
		//查询参数
		Map<String,Object> where  = new HashMap<String,Object>();
		if (request.getSelectKey()>0&&StringUtils.isNotBlank(request.getSelectVal())) {
			where.put("selectKey",request.getSelectKey());
			where.put("selectValue",request.getSelectVal());
		}
		//排序
		if(StringUtils.isBlank(request.getOrderBy())){
			request.setOrderBy(" createTime DESC");
		}
		
		Long resultCnt = sysUserMapper.queryPagerCount(where);
		if(null!=resultCnt&&resultCnt.longValue()==0){
			resultPager.setTotalCount(0);
			return resultPager;
		}
		
		List<SysUserDto> list = sysUserMapper.querySysUserPager(where,request.getOrderBy(),(request.getPageIndex()-1)*request.getPageSize(), request.getPageSize());
		//查询返回机构下的用户
		if(CollectionUtils.isNotEmpty(list)){
			resultPager.setData(new ArrayList<QuerySysUsersResponse>());
			QuerySysUsersResponse infoRes = null;
			try{
				for(SysUserDto info : list){
					infoRes = new QuerySysUsersResponse();
					BeanUtils.copyProperties(infoRes, info);
					resultPager.getData().add(infoRes);
				}
			}catch(Exception e){
				log.error("企业信息查询组合list异常",e);
			}finally{
				list.clear();
				where.clear();
			}
		}
		//设置查询条数
		resultPager.setTotalCount(resultCnt);
		resultPager.setPager();
		where.clear();
		return resultPager;
	}

	public Pager<QuerySysUsersResponse> queryOrgUserPager(SysUserPagerRequest request, Client client) {
		Pager<QuerySysUsersResponse> resultPager = request.newPager(QuerySysUsersResponse.class);
		
		//查询参数
		Map<String,Object> where  = new HashMap<String,Object>();
		where.put("orgCode", client.getOrgCode());
		if (request.getSelectKey()>0&&StringUtils.isNotBlank(request.getSelectVal())) {
			where.put("selectKey",request.getSelectKey());
			where.put("selectValue",request.getSelectVal());
		}
		if (null!=request.getDepartCode()&&request.getDepartCode().longValue()>0) {
			where.put("departCode",request.getDepartCode());										//部门
		}
		
		//排序
		if(StringUtils.isBlank(request.getOrderBy())){
			request.setOrderBy("createTime DESC");
		}
		
		Long resultCnt = sysUserMapper.queryOrgUserPagerCount(where);
		if(null!=resultCnt&&resultCnt.longValue()==0){
			resultPager.setTotalCount(0);
			return resultPager;
		}
		
		List<SysUserDto> list = sysUserMapper.queryOrgUserPager(where,request.getOrderBy(),(request.getPageIndex()-1)*request.getPageSize(), request.getPageSize());
		//查询返回机构下的用户
		if(CollectionUtils.isNotEmpty(list)){
			resultPager.setData(new ArrayList<QuerySysUsersResponse>());
			QuerySysUsersResponse infoRes = null;
			try{
				for(SysUserDto info : list){
					infoRes = new QuerySysUsersResponse();
					BeanUtils.copyProperties(infoRes, info);
					//普通员工查询,隐藏身份证,隐藏手机号
					if (client.getRoleId()==1&&StringUtils.isNotBlank(infoRes.getIdCard())){
						String idCard = infoRes.getIdCard();
						idCard.replace(idCard.substring(4, 15), "***********");
						infoRes.setIdCard(idCard);
					}
					resultPager.getData().add(infoRes);
				}
			}catch(Exception e){
				log.error("企业信息查询组合list异常",e);
			}finally{
				list.clear();
				where.clear();
			}
		}
		//设置查询条数
		resultPager.setTotalCount(resultCnt);
		resultPager.setPager();
		where.clear();
		return resultPager;
	}

	public Long getUserCodeByUnionId(String unionId) {
		return sysUserMapper.getUserCodeByUnion(unionId);
	}

	public OrgUserSaoInfoWayResponse getOrgUserSaoInfo(long orgCode, long userCode) {
		OrgUserSaoInfoWayResponse result = null;
		if(userCode==0||orgCode==0)return result;//用户编号不合法
		UserNameAndImgUrlDto uImg = sysUserMapper.getUserNameByCode(userCode);
		if(null==uImg)return result;//用户不存在
		//XbdunSysOrgDo sinlgeOrgInfo1 = orgInfoCacheService.getSinlgeOrgInfo(orgCode);
		
		SysOrgFiledsDto orgFilds = orgInfoCacheService.getOrgFildes(orgCode);
		//机构
		//CardOrgInfoDto cardOrgInfo02 = sysOrgMapper.getCardOrgInfo02(orgCode);
		if(null==orgFilds){ 
			uImg = null;
			return result;
		}
		result = new OrgUserSaoInfoWayResponse();
		//用户信息
		result.setUserName(uImg.getuName());
		result.setImgUrl(uImg.getImgUrl());//头像
		result.setGamUrl(uImg.getImgUrlFls());//社交头像
		//机构信息
		result.setOrgName(orgFilds.getOrgName());//公司机构名称
		result.setOrgResume(orgFilds.getOrgResume());//公司详情
		result.setMainCamp(orgFilds.getMainCamp());//机构经营介绍
		//用户对应机构角色
		List<String> orgUserDepartRole = orgUserMapper.getOrgUserDepartRole(userCode,orgCode);
		if(CollectionUtils.isNotEmpty(orgUserDepartRole)){
			//合并所在企业下的角色
			result.setDepart(com.alibaba.dubbo.common.utils.CollectionUtils.join(orgUserDepartRole, ","));
			orgUserDepartRole.clear();
		}
		return result;
	}
}
